package com.atwork.wuhua.ui.activity;

import android.os.Bundle;
import android.util.TimeUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.atwork.wuhua.R;
import com.atwork.wuhua.base.BaseActivity;
import com.atwork.wuhua.bean.UserBean;
import com.atwork.wuhua.event.LoginSuccessEvent;
import com.atwork.wuhua.mvp.presenter.LoginPresenter;
import com.atwork.wuhua.mvp.view.ILoginActivity;
import com.atwork.wuhua.utils.TimeUtil;
import com.atwork.wuhua.utils.UserUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

public class LoginActivity extends BaseActivity implements ILoginActivity {
    @BindView(R.id.ed_phone)
    EditText edPhone;
    @BindView(R.id.ed_code)
    EditText edCode;
    @BindView(R.id.tv_get_code)
    TextView tvGetCode;

    private LoginPresenter presenter;

    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        presenter = new LoginPresenter();
        presenter.attachView(this);
    }

    @Override
    public void initData() {

    }


    @OnClick({R.id.tv_get_code, R.id.tv_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code:
                presenter.getVerify(this, tvGetCode, edPhone.getText().toString());
                break;
            case R.id.tv_login:
                presenter.login(edPhone.getText().toString(), edCode.getText().toString());
                break;
        }
    }

    @Override
    public void setLogin() {
        EventBus.getDefault().post(new LoginSuccessEvent());
        showToast("登录成功");
        Observable.timer(1, TimeUnit.SECONDS)
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {

                        finish();
                    }
                });
    }

    @Override
    public void setVerify(String verify) {
        edCode.setText(verify);
    }

}
